package dao;

import java.sql.Time;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import entity.Contractor;
import entity.Employee;
import entity.TimeCard;

@Stateless
public class TimeCardDao {
	
	@PersistenceContext
	EntityManager manager;
	
	//create time card
	public TimeCard CreateTimeCard(Date date, Time start, Time end,Contractor employee){
		TimeCard card=new TimeCard(date, start, end,employee);
		manager.persist(card);
		return card;
	}
	
	//finds a specific time card
	public TimeCard findCard(int id){
		TimeCard card=manager.find(TimeCard.class, id);
		return card;
	}
	
	//finds a time card of a specific employee
	public List<TimeCard> listTime(Contractor employee){
		String spql="Select t from TimeCard t where t.contractor='" + employee.getId()+ "'";
		TypedQuery<TimeCard> query=manager.createQuery(spql, TimeCard.class);
		List<TimeCard> listTime=query.getResultList();
		return listTime;
	}
	
	//delete time card
	public int deleteTimeCard(TimeCard card){
		try{
			TimeCard deleteCard=manager.merge(card);
			manager.remove(deleteCard);
			return 0;
		} catch (Exception e){
			return -1;
		}
	}

}
